#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "los_tick.h"
#include "los_swtmr.h"

UINT32 t_cnt1, t_cnt2;
UINT32 id1, id2;

void swt1_cb(UINT32 arg){
    printf("swt1 timeout %d\n", t_cnt1++);
}

void swt2_cb(UINT32 arg){
    UINT32 tick;
    // 获取定时器剩余tick
    LOS_SwtmrTimeGet(id2, &tick);
    printf("swt2 timeout %d time get %d\n", t_cnt2++, tick);
    // 停止定时器
    if(t_cnt2 > 5)
        LOS_SwtmrStop(id2);
}

void swt_entry(){
    UINT32 ret;

    // 创建一次性定时器
    ret = LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, swt1_cb, &id1, 0, 0, 0);
    if(ret != LOS_OK) { printf("create swt1 error\n"); return; }
    // 创建周期性定时器
    ret = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, swt2_cb, &id2, 0, 0, 0);
    if(ret != LOS_OK) { printf("create swt2 error\n"); return; }

    //启动定时器
    LOS_SwtmrStart(id1);
    LOS_SwtmrStart(id2);
}

SYS_RUN(swt_entry);
